package com.xiangge.aliyun.aliyuntest.controller;


import com.xiangge.aliyun.aliyuntest.entity.ExpressInfo;
import com.xiangge.aliyun.aliyuntest.enums.ShippingEnum;
import com.xiangge.aliyun.aliyuntest.service.ExpressService;
import com.xiangge.aliyun.aliyuntest.util.ResponseUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author shaoxiang
 */
@RestController
@RequestMapping("/shipping/")
@Api(tags = "物流信息相关")
public class ShippingController {
    @Autowired
    private ExpressService expressService;

    @GetMapping("{type}/{code}")
    @ApiOperation(value = "物流查询", notes = "物流查询", produces = "application/json")
    public Object get(@ApiParam(required = true, value = "物流类型") @PathVariable(required = true) Integer type,
                      @ApiParam(required = true, value = "物流码") @PathVariable(required = true) String code) {
        ShippingEnum shipType = ShippingEnum.getByType(type);
        if (shipType == null) {
            return ResponseUtil.fail(700, "物流暂不支持");
        }
        ExpressInfo expressInfo;
        try {
            expressInfo = expressService.getExpressInfo(shipType, code);
        } catch (Exception e) {
            return ResponseUtil.fail();
        }
        return ResponseUtil.ok(expressInfo);
    }
}
